package com.xb.sourest.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ocnyang.cartlayout.bean.ICartItem;
import com.xb.sourest.R;
import com.xb.sourest.bean.GoodsItem;
import com.xb.sourest.bean.ShopGoodsBean;
import com.xb.sourest.view.NumberAddSubtractLayout;
import com.xd.navi.GlideUtils;
import com.xd.navi.PhoneUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class CarAdapter extends RecyclerView.Adapter<CarAdapter.CartHolder> {

    private Context mContext;
    private List<GoodsItem> goodsItemList;

    public CarAdapter(Context context,List<GoodsItem>   goodsItems){
        this.mContext= context;
        this.goodsItemList = goodsItems;
    }


    @NonNull
    @Override
    public CartHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View childView = LayoutInflater.from(mContext).inflate(R.layout.shop_item_child, parent, false);
        return new CartHolder(childView);
    }

    @Override
    public void onBindViewHolder(@NonNull CartHolder holder, int position) {

        holder.textView.setText(( goodsItemList.get(position)).getGoodsName());
        holder.textViewPrice.setText("￥" +  PhoneUtils.fromatAmount((goodsItemList.get(position)).getPrice()));
        GlideUtils.load(goodsItemList.get(position).getImage(),holder.draw_goods,R.mipmap.icon_error);
        holder.addSubtractLayout.setCurrentNumber(goodsItemList.get(position).getNumber());
        holder.tv_type.setText(goodsItemList.get(position).getGoodsRules());

        holder.checkbox.setChecked(goodsItemList.get(position).isChecked());

        //商品数量操作
        holder.addSubtractLayout.setChangeNumberListener(new NumberAddSubtractLayout.ChangeNumberListener() {
            @Override
            public void onChangeNumber(int number) {
                GoodsItem goodsItem = goodsItemList.get(position);
                goodsItem.setNumber(number);
                if (onCheckChangeListener != null){
                    onCheckChangeListener.onCaulueCallBack(goodsItemList);
                }
            }
        });

        //复选框
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                goodsItemList.get(position).setChecked(isChecked);
                if (onCheckChangeListener != null){
                    onCheckChangeListener.onCaulueCallBack(goodsItemList);
                }
            }
        });

        holder.frame_layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //TodO()长按事件
                if (onCheckChangeListener != null){
                    onCheckChangeListener.onLongClick(goodsItemList,v,position);
                }
                return true;
            }
        });

    }

    /**
     * 全选操作
     * @param isCheck
     */
    public  void checkAll(boolean isCheck){
        boolean noOneChange = true;
        for(int i = 0; i < this.goodsItemList.size(); ++i) {
            if ((this.goodsItemList.get(i)).isChecked() != isCheck) {
                goodsItemList.get(i).setChecked(isCheck);
                noOneChange = false;
            }
        }
        if (!noOneChange) {
            this.notifyDataSetChanged();
            if (this.onCheckChangeListener != null) {
                this.onCheckChangeListener.onCaulueCallBack(goodsItemList);
            }
        }
    }

    public  void delete(int position){
        if (goodsItemList!= null && !goodsItemList.isEmpty()){
            Log.e("fragment--","position:" + position);

            goodsItemList.remove(position);
            this.notifyItemRemoved(position);
            this.notifyDataSetChanged();
            if (onCheckChangeListener != null){
                onCheckChangeListener.onCaulueCallBack(goodsItemList);
            }
        }
    }

    @Override
    public int getItemCount() {
        return goodsItemList.size();
    }

    public class  CartHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv)
         TextView textView;
        @BindView(R.id.tv_price)
         TextView textViewPrice;
        @BindView(R.id.tv_type)
         TextView tv_type;
        @BindView(R.id.draw_goods)
         ImageView draw_goods;
        @BindView(R.id.number_addsubtract_button_default)
         NumberAddSubtractLayout addSubtractLayout;
        @BindView(R.id.checkbox)
         CheckBox checkbox;
        @BindView(R.id.frame_layout)
        FrameLayout frame_layout;

        public CartHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

            frame_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onCheckChangeListener != null){
                        onCheckChangeListener.onItemClick();
                    }
                }
            });

        }
    }

    protected OnCheckChangeListener onCheckChangeListener;
    public  interface  OnCheckChangeListener{
        void  onCaulueCallBack(List<GoodsItem> goodsItems);

        void  onLongClick(List<GoodsItem> goodsItems,View view,int position);

        void  onItemClick();
    }
    public void  setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener){
        this.onCheckChangeListener = onCheckChangeListener;
    }

}
